/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.fastcase.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.constant.PunishMainServicePortUriConstant;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.common.utils.HeaderUtil;
import com.icinfo.cloud.provider.common.utils.PunishMainClientUtil;
import com.icinfo.cloud.provider.exception.BusinessException;
import com.icinfo.cloud.provider.punish.fastcase.dto.AppCaseAppraiseDetailInfoSaveDto;
import com.icinfo.cloud.provider.punish.fastcase.dto.AppCaseAppraiseRecordByPartyQueryDto;
import com.icinfo.cloud.provider.punish.fastcase.dto.CaseAppraiseRecordByPartyQueryDto;
import com.icinfo.cloud.provider.punish.fastcase.enums.CaseAppraiseStatusEnum;
import com.icinfo.cloud.provider.punish.fastcase.mapper.CaseAppraiseRecordMapper;
import com.icinfo.cloud.provider.punish.fastcase.mapper.CaseAppraiseTemplateInfoMapper;
import com.icinfo.cloud.provider.punish.fastcase.model.CaseAppraiseRecord;
import com.icinfo.cloud.provider.punish.fastcase.model.CaseAppraiseTemplateInfo;
import com.icinfo.cloud.provider.punish.fastcase.service.ICaseAppraiseRecordService;
import com.icinfo.cloud.provider.punish.fastcase.vo.*;
import com.icinfo.cloud.provider.punish.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 案件评价记录表 case_appraise_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2023年02月02日
 */
@Slf4j
@Service
public class CaseAppraiseRecordServiceImpl extends MyBatisServiceSupport implements ICaseAppraiseRecordService {

    @Resource
    private CaseAppraiseRecordMapper caseAppraiseRecordMapper;
    @Resource
    private CaseAppraiseTemplateInfoMapper caseAppraiseTemplateInfoMapper;
    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;

    /**
     * 从请求头中获取用户idnum
     *
     * @return
     */
    private String getUserIdNum() {
        Map<String, String> headers = HeaderUtil.headerInfo();
        //log.info("headers:{}", headers);
        //从请求头中获取用户idnum
        String partyIdentityCode = headers.get("idnum");
        if (ObjectUtils.isEmpty(partyIdentityCode)) {
            throw new BusinessException("获取用户信息失败");
        }
        return partyIdentityCode;
    }

    /**
     * 描述： 根据当事人身份编码获取待评价记录数
     *
     * @author: zhengqiang
     * @date: 2023/2/6
     * @return {@link Result}
     **/
    @Override
    public Result getCountOfNoAppraiseRecordByParty() {
        String partyIdentityCode = getUserIdNum();
        Integer count = caseAppraiseRecordMapper.selectCountOfNoAppraiseRecordByParty(partyIdentityCode);
        return Result.success(count);
    }

    /**
     * 描述： 根据当事人身份编码获取不同评价列表记录数
     *
     * @author: zhengqiang
     * @date: 2023/2/8
     * @return {@link Result< CaseAppraiseRecordCountVo>}
     **/
    @Override
    public Result<CaseAppraiseRecordCountVo> getCountOfCaseAppraiseRecordByQueryListType() {
        CaseAppraiseRecordCountVo caseAppraiseRecordCountVo = new CaseAppraiseRecordCountVo();
        String partyIdentityCode = getUserIdNum();
        Integer noAppraiseCount = caseAppraiseRecordMapper.selectCountOfCaseAppraiseRecordByQueryListType(partyIdentityCode, "1");
        Integer noFeedbackCount = caseAppraiseRecordMapper.selectCountOfCaseAppraiseRecordByQueryListType(partyIdentityCode, "2");
        Integer feedbackCount = caseAppraiseRecordMapper.selectCountOfCaseAppraiseRecordByQueryListType(partyIdentityCode, "3");
        caseAppraiseRecordCountVo.setNoAppraiseCount(noAppraiseCount);
        caseAppraiseRecordCountVo.setNoFeedbackCount(noFeedbackCount);
        caseAppraiseRecordCountVo.setFeedbackCount(feedbackCount);
        return Result.success(caseAppraiseRecordCountVo);
    }

    /**
     * 描述： 查询案件评价记录
     *
     * @author: zhengqiang
     * @date: 2023/2/3
     * @param: partyQueryDto
     * @return {@link List< CaseAppraiseRecordVo>}
     **/
    @Override
    public Result<PageResponse<CaseAppraiseRecordVo>> getCaseAppraiseRecordPageList(AppCaseAppraiseRecordByPartyQueryDto partyQueryDto) {
        String partyIdentityCode = getUserIdNum();
        CaseAppraiseRecordByPartyQueryDto queryDto = new CaseAppraiseRecordByPartyQueryDto();
        BeanUtils.copyProperties(partyQueryDto, queryDto);
        queryDto.setPartyIdentityCode(partyIdentityCode);
        queryDto.setCaseReason(partyQueryDto.getSearchValue());
        Result result = PunishMainClientUtil.httpPost(PunishMainServicePortUriConstant.GET_CASE_APPRAISE_RECORD_PAGE_LIST_BY_PARTY, queryDto);
        return result;
    }

    /**
     * 描述： 根据案件id获取案件评价详情
     *
     * @author: zhengqiang
     * @date: 2023/2/3
     * @param: caseId
     * @return {@link Result< CaseAppraiseDetailInfoVo>}
     **/
    @Override
    public Result<CaseAppraiseDetailInfoVo> getCaseAppraiseRecordDetailByCaseId(String caseId) {
        Map<String, Object> urlParam = new HashMap<>();
        urlParam.put("caseId", caseId);
        Result result = PunishMainClientUtil.httpGet(PunishMainServicePortUriConstant.GET_CASE_APPRAISE_RECORD_DETAIL_BY_CASE_ID, urlParam);
        return result;
    }

    /**
     * 描述： 保存评价信息
     *
     * @author: zhengqiang
     * @date: 2023/2/3
     * @param: caseAppraiseDetailInfoVo
     * @return {@link Result}
     **/
    @Override
    public Result saveCaseAppraiseRecord(AppCaseAppraiseDetailInfoSaveDto appCaseAppraiseDetailInfoSaveDto) {
        String caseId = appCaseAppraiseDetailInfoSaveDto.getCaseId();
        Example appraiseExample = new Example(CaseAppraiseRecord.class);
        appraiseExample.createCriteria().andEqualTo("caseId", appCaseAppraiseDetailInfoSaveDto.getCaseId());
        List<CaseAppraiseRecord> caseAppraiseRecords = caseAppraiseRecordMapper.selectByExample(appraiseExample);
        CaseAppraiseRecord caseAppraiseRecord = new CaseAppraiseRecord();
        if (ObjectUtils.isEmpty(caseAppraiseRecords)) {
            caseAppraiseRecord.setId(BaseUtil.createUid());
            caseAppraiseRecord.setCaseId(appCaseAppraiseDetailInfoSaveDto.getCaseId());

            CaseMainInfo caseMainInfo = caseMainInfoMapper.selectByPrimaryKey(caseId);
            if (ObjectUtils.isEmpty(caseMainInfo)) {
                throw new BusinessException("案件不存在");
            }

            Example example = new Example(CaseAppraiseTemplateInfo.class);
            example.createCriteria().andEqualTo("applyCaseType", caseMainInfo.getCaseType()).andEqualTo("isDelete", Constant.STRING_0);
            List<CaseAppraiseTemplateInfo> caseAppraiseTemplateInfos = caseAppraiseTemplateInfoMapper.selectByExample(example);
            if (ObjectUtils.isEmpty(caseAppraiseTemplateInfos)) {
                throw new BusinessException("案件评价模板不存在");
            }
            CaseAppraiseTemplateInfo caseAppraiseTemplateInfo = caseAppraiseTemplateInfos.get(0);
            caseAppraiseRecord.setAppraiseTemplateId(caseAppraiseTemplateInfo.getId());

        } else {
            caseAppraiseRecord = caseAppraiseRecords.get(0);
        }


        Map<String, String> valueMap = appCaseAppraiseDetailInfoSaveDto.getValueMap();
        //判断是否存在异常
        String isExistException = YesOrNoEnum.NO.getCode();
        for (Map.Entry<String, String> entry : valueMap.entrySet()) {
            if (YesOrNoEnum.NO.getCode().equals(entry.getValue())) {
                isExistException = YesOrNoEnum.YES.getCode();
                break;
            }
        }
        caseAppraiseRecord.setIsExistException(isExistException);
        caseAppraiseRecord.setAppraiseContent(JSONObject.toJSONString(appCaseAppraiseDetailInfoSaveDto));
        caseAppraiseRecord.setAppraiseStatus(CaseAppraiseStatusEnum.NO_FEEDBACK.getCode());
        int i = 0;
        if (ObjectUtils.isEmpty(caseAppraiseRecords)) {
            i = caseAppraiseRecordMapper.insertSelective(caseAppraiseRecord);
        } else {
            i = caseAppraiseRecordMapper.updateByExampleSelective(caseAppraiseRecord, appraiseExample);
        }
        if (i > 0) {
            return Result.success(Constant.STRING_SUCCESS_EN);
        } else {
            return Result.error(Constant.STRING_FAIL_EN);
        }
    }

    /**
     * 描述： 根据案件类型获取评价模板
     *
     * @author: zhengqiang
     * @date: 2023/2/6
     * @param: caseType
     * @return {@link Result< List<  CaseAppraiseTemplateGroupInfoVo >>}
     **/
    @Override
    public Result<List<CaseAppraiseTemplateGroupInfoVo>> getCaseAppraiseTemplateInfo(String caseType) {
        Map<String, Object> urlParam = new HashMap<>();
        urlParam.put("caseType", caseType);
        Result result = PunishMainClientUtil.httpGet(PunishMainServicePortUriConstant.GET_CASE_APPRAISE_TEMPLATE_INFO, urlParam);
        return result;
    }

    /**
     * 描述： 根据当事人身份编码查询进行中的速结案件记录
     *
     * @author: zhengqiang
     * @date: 2023/2/8
     * @return {@link List}
     **/
    @Override
    public List<AppFastCaseRecordOfHandlingVo> getFastCaseRecordListOfHandlingByParty() {
        String partyIdentityCode = getUserIdNum();
        List<AppFastCaseRecordOfHandlingVo> appFastCaseRecordOfHandlingVos = caseMainInfoMapper.selectFastCaseRecordListOfHandlingByParty(partyIdentityCode);
        return appFastCaseRecordOfHandlingVos;
    }

}